package com.taixu.business.controller;


import cn.hutool.core.util.ObjectUtil;
import com.taixu.apikey.domain.bo.ApiKeyBo;
import com.taixu.apikey.domain.vo.ApiKeyVo;
import com.taixu.apikey.service.IApiKeyService;
import com.taixu.apiorder.domain.ApiOrder;
import com.taixu.apiorder.domain.bo.ApiOrderBo;
import com.taixu.apiorder.domain.vo.ApiOrderVo;
import com.taixu.apiorder.service.IApiOrderService;
import com.taixu.common.core.domain.R;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @projectName: taixu
 * @package: com.taixu.business.controller
 * @className: BusinessController
 * @author: ximu
 * @date: 2023/12/13 22:35
 * @version: 1.0
 */

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/che")
public class BusinessController {

    private final IApiOrderService iApiOrderService;

    private final IApiKeyService iApiKeyService;

    @PostMapping("/viewstatus")
    public R<ApiOrderVo> viewstatus(String orderNo,String key) {

        ApiOrderVo apiOrderVo = iApiOrderService.queryByOrder(orderNo);
        if(ObjectUtil.isNull(apiOrderVo)){
            apiOrderVo = iApiOrderService.queryById(Long.valueOf(orderNo));
        }
        if(ObjectUtil.isNull(apiOrderVo)){
            return R.fail("该订单不存在");
        }
        //判断key
        if(!apiOrderVo.getBykey().equals(key)){
            return R.fail("该订单非此key创建");
        }
        ApiKeyVo apiKeyVo = iApiKeyService.queryByKey(key);
        if(apiKeyVo.getResidue()<=0){
            return R.fail("该key使用次数已耗尽，请联系管理员");
        }else{
            Long residue = apiKeyVo.getResidue();
            apiKeyVo.setResidue( -- residue);
            ApiKeyBo apiKeyBo = new ApiKeyBo();
            BeanUtils.copyProperties(apiKeyVo,apiKeyBo);
            iApiKeyService.updateByBo(apiKeyBo);
        }
        return R.ok("查询成功",apiOrderVo);
    }

    @PostMapping("/statusorder")
    public R<ApiOrderVo> createOrder(String vin ,String key){
        if ( vin == null||vin.equals("")){
            return R.fail("请填写车架号");
        }
        ApiKeyVo apiKeyVo = iApiKeyService.queryByKey(key);
        if (ObjectUtil.isNull(apiKeyVo)){
            return R.fail("key不存在，请检查输入是否正确，如若无误请联系管理员");
        }
        if (apiKeyVo.getResidue()<=0){
            return R.fail("该key使用次数已耗尽，请联系管理员");
        }
        ApiOrderBo apiOrder = new ApiOrderBo();
        apiOrder.setVin(vin);
        apiOrder.setStatus(30L);
        apiOrder.setResStatus(10L);
        apiOrder.setPushStatus(20L);
        apiOrder.setBykey(key);
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意月和小时的格式为两个大写字母
        java.util.Date date = new Date();//获得当前时间
        apiOrder.setFoundTime(date);

        //需要回调刚刚查的数据
        Boolean rs = iApiOrderService.insertByBo(apiOrder);
        ApiOrderVo apiOrderVo;
        if (rs){
            Long orderId = apiOrder.getOrderId();
            apiOrderVo = iApiOrderService.queryById(orderId);
            apiOrderVo.setOrderNo("STS"+apiOrderVo.getOrderId());
            ApiOrderBo apiOrderBo = new ApiOrderBo();
            BeanUtils.copyProperties(apiOrderVo,apiOrderBo);
            iApiOrderService.updateByBo(apiOrderBo);
            return R.ok(apiOrderVo);
        }else {
            return R.fail("数据插入异常，请联系管理员");
        }


    }


}
